<?php
/**
 * Created by PhpStorm.
 * User: 陈阳
 * Date: 2021/1/31
 * Time: 8:33
 */
namespace app\admin\model;
use think\model;
class Order extends Model
{

    public function getAddTimeAttr($value)
    {
        return date('Y-n-j H:i:s',$value);
    }
    public function searchDindanAttr($query,$value){
        if(''!==$value){
            $query->where('order_sn','like','%'.$value.'%');
        }
    }
    public function searchStatusAttr($query,$value){
        if(is_array($value) && !empty($value)){
            $query->where('o.status','in',$value);
       }
    }
    public function searchMaxJiaAttr($query,$value){
        if(!empty($value)){
            $query->where('goods_amount','<=',$value);
        }
    }
    public function searchMinJiaAttr($query,$value){
        if(!empty($value)){
            $query->where('goods_amount','>=',$value);
        }
    }
    public static function dinDan($data = [],$query = [], $page = true){
        $field=[
            'o.order_id','buyer_name','goods_amount','add_time','postscript','order_sn','status',
            'g.changed_status','p.payment_id','p.payment_name',
        ];
        $model = self::field($field)->alias("o")
                                 ->withSearch(array_keys($data),$data)
                                 ->join(['ecm_order_log'=>'g'],'o.order_id=g.order_id','LEFT')
                                 ->join(['ecm_payment'=>'p'],'o.payment_id=p.payment_id','LEFT');

        if($page){
            $list_rows = \think\facade\Config::has('view.list_rows')?\think\facade\Config::get('view.list_rows'):15;
            return $model->paginate([
                'list_rows' => $list_rows,//分页的配置参数
                'query'     => $query,
            ]);
        }else{
            return $model->select();
        }

    }




    public static function dinDant($data = [],$query = [], $page = true){
        $field=[
            'o.order_id','buyer_name','goods_amount','add_time','postscript','order_sn','status',
            'g.changed_status','p.payment_id','p.payment_name',
        ];
        $model = self::field($field)->alias("o")
            ->join(['ecm_order_log'=>'g'],'o.order_id=g.order_id','LEFT')
            ->join(['ecm_payment'=>'p'],'o.payment_id=p.payment_id','LEFT');
        if(isset($data['dindan']) && '' !== $data['dindan']){
            $model->whereLike('order_sn','%'.$data['dindan'].'%');
        }
        if(is_array($data['status']) && !empty($data['status'])){
            $model->where('o.status','in',$data['status']);
       }
        if(isset($data['maxjia']) && '' !== $data['maxjia']){
            $model->where('goods_amount','<=',$data['maxjia']);
        }
        if(isset($data['$minjia']) && '' !== $data['$minjia']){
            $model->where('goods_amount','<=',$data['$minjia']);
        }

        if($page){
            $list_rows = \think\facade\Config::has('view.list_rows')?\think\facade\Config::get('view.list_rows'):15;
            return $model->paginate([
                'list_rows' => $list_rows,//分页的配置参数
                'query'     => $query,
            ]);
        }else{
            return $model->select();
        }

    }
}